/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2015 Joyent, Inc.
 */

/*
 * This file contains a mapping table and lookup function for converting
 * illumos native error numbers into error numbers appropriate for Linux
 * emulation.
 *
 * The translation table is generated by the "gen_errno", built from and
 * documented in "usr/src/common/brand/lx/tools".
 */

#include <sys/debug.h>

const int
lx_stol_errno[] = {
	0,	/*   0: No Error					*/
	1,	/*   1: EPERM		-->   1: EPERM			*/
	2,	/*   2: ENOENT		-->   2: ENOENT			*/
	3,	/*   3: ESRCH		-->   3: ESRCH			*/
	4,	/*   4: EINTR		-->   4: EINTR			*/
	5,	/*   5: EIO		-->   5: EIO			*/
	6,	/*   6: ENXIO		-->   6: ENXIO			*/
	7,	/*   7: E2BIG		-->   7: E2BIG			*/
	8,	/*   8: ENOEXEC		-->   8: ENOEXEC		*/
	9,	/*   9: EBADF		-->   9: EBADF			*/
	10,	/*  10: ECHILD		-->  10: ECHILD			*/
	11,	/*  11: EAGAIN		-->  11: EAGAIN			*/
	12,	/*  12: ENOMEM		-->  12: ENOMEM			*/
	13,	/*  13: EACCES		-->  13: EACCES			*/
	14,	/*  14: EFAULT		-->  14: EFAULT			*/
	15,	/*  15: ENOTBLK		-->  15: ENOTBLK		*/
	16,	/*  16: EBUSY		-->  16: EBUSY			*/
	17,	/*  17: EEXIST		-->  17: EEXIST			*/
	18,	/*  18: EXDEV		-->  18: EXDEV			*/
	19,	/*  19: ENODEV		-->  19: ENODEV			*/
	20,	/*  20: ENOTDIR		-->  20: ENOTDIR		*/
	21,	/*  21: EISDIR		-->  21: EISDIR			*/
	22,	/*  22: EINVAL		-->  22: EINVAL			*/
	23,	/*  23: ENFILE		-->  23: ENFILE			*/
	24,	/*  24: EMFILE		-->  24: EMFILE			*/
	25,	/*  25: ENOTTY		-->  25: ENOTTY			*/
	26,	/*  26: ETXTBSY		-->  26: ETXTBSY		*/
	27,	/*  27: EFBIG		-->  27: EFBIG			*/
	28,	/*  28: ENOSPC		-->  28: ENOSPC			*/
	29,	/*  29: ESPIPE		-->  29: ESPIPE			*/
	30,	/*  30: EROFS		-->  30: EROFS			*/
	31,	/*  31: EMLINK		-->  31: EMLINK			*/
	32,	/*  32: EPIPE		-->  32: EPIPE			*/
	33,	/*  33: EDOM		-->  33: EDOM			*/
	34,	/*  34: ERANGE		-->  34: ERANGE			*/
	42,	/*  35: ENOMSG		-->  42: ENOMSG			*/
	43,	/*  36: EIDRM		-->  43: EIDRM			*/
	44,	/*  37: ECHRNG		-->  44: ECHRNG			*/
	45,	/*  38: EL2NSYNC	-->  45: EL2NSYNC		*/
	46,	/*  39: EL3HLT		-->  46: EL3HLT			*/
	47,	/*  40: EL3RST		-->  47: EL3RST			*/
	48,	/*  41: ELNRNG		-->  48: ELNRNG			*/
	49,	/*  42: EUNATCH		-->  49: EUNATCH		*/
	50,	/*  43: ENOCSI		-->  50: ENOCSI			*/
	51,	/*  44: EL2HLT		-->  51: EL2HLT			*/
	35,	/*  45: EDEADLK		-->  35: EDEADLK		*/
	37,	/*  46: ENOLCK		-->  37: ENOLCK			*/
	125,	/*  47: ECANCELED	--> 125: ECANCELED		*/
	38,	/*  48: ENOTSUP		-->  38: ENOSYS			*/
	122,	/*  49: EDQUOT		--> 122: EDQUOT			*/
	52,	/*  50: EBADE		-->  52: EBADE			*/
	53,	/*  51: EBADR		-->  53: EBADR			*/
	54,	/*  52: EXFULL		-->  54: EXFULL			*/
	55,	/*  53: ENOANO		-->  55: ENOANO			*/
	56,	/*  54: EBADRQC		-->  56: EBADRQC		*/
	57,	/*  55: EBADSLT		-->  57: EBADSLT		*/
	35,	/*  56: EDEADLOCK	-->  35: EDEADLK		*/
	59,	/*  57: EBFONT		-->  59: EBFONT			*/
	130,	/*  58: EOWNERDEAD	--> 130: EOWNERDEAD		*/
	131,	/*  59: ENOTRECOVERABLE	--> 131: ENOTRECOVERABLE	*/
	60,	/*  60: ENOSTR		-->  60: ENOSTR			*/
	61,	/*  61: ENODATA		-->  61: ENODATA		*/
	62,	/*  62: ETIME		-->  62: ETIME			*/
	63,	/*  63: ENOSR		-->  63: ENOSR			*/
	64,	/*  64: ENONET		-->  64: ENONET			*/
	65,	/*  65: ENOPKG		-->  65: ENOPKG			*/
	66,	/*  66: EREMOTE		-->  66: EREMOTE		*/
	67,	/*  67: ENOLINK		-->  67: ENOLINK		*/
	68,	/*  68: EADV		-->  68: EADV			*/
	69,	/*  69: ESRMNT		-->  69: ESRMNT			*/
	70,	/*  70: ECOMM		-->  70: ECOMM			*/
	71,	/*  71: EPROTO		-->  71: EPROTO			*/
	-2,	/*  72: ELOCKUNMAPPED	-->  -2: No Analogue		*/
	-2,	/*  73: ENOTACTIVE	-->  -2: No Analogue		*/
	72,	/*  74: EMULTIHOP	-->  72: EMULTIHOP		*/
	-1,	/*  75: Unused Number					*/
	-1,	/*  76: Unused Number					*/
	74,	/*  77: EBADMSG		-->  74: EBADMSG		*/
	36,	/*  78: ENAMETOOLONG	-->  36: ENAMETOOLONG		*/
	75,	/*  79: EOVERFLOW	-->  75: EOVERFLOW		*/
	76,	/*  80: ENOTUNIQ	-->  76: ENOTUNIQ		*/
	77,	/*  81: EBADFD		-->  77: EBADFD			*/
	78,	/*  82: EREMCHG		-->  78: EREMCHG		*/
	79,	/*  83: ELIBACC		-->  79: ELIBACC		*/
	80,	/*  84: ELIBBAD		-->  80: ELIBBAD		*/
	81,	/*  85: ELIBSCN		-->  81: ELIBSCN		*/
	82,	/*  86: ELIBMAX		-->  82: ELIBMAX		*/
	83,	/*  87: ELIBEXEC	-->  83: ELIBEXEC		*/
	84,	/*  88: EILSEQ		-->  84: EILSEQ			*/
	38,	/*  89: ENOSYS		-->  38: ENOSYS			*/
	40,	/*  90: ELOOP		-->  40: ELOOP			*/
	85,	/*  91: ERESTART	-->  85: ERESTART		*/
	86,	/*  92: ESTRPIPE	-->  86: ESTRPIPE		*/
	39,	/*  93: ENOTEMPTY	-->  39: ENOTEMPTY		*/
	87,	/*  94: EUSERS		-->  87: EUSERS			*/
	88,	/*  95: ENOTSOCK	-->  88: ENOTSOCK		*/
	89,	/*  96: EDESTADDRREQ	-->  89: EDESTADDRREQ		*/
	90,	/*  97: EMSGSIZE	-->  90: EMSGSIZE		*/
	91,	/*  98: EPROTOTYPE	-->  91: EPROTOTYPE		*/
	92,	/*  99: ENOPROTOOPT	-->  92: ENOPROTOOPT		*/
	-1,	/* 100: Unused Number					*/
	-1,	/* 101: Unused Number					*/
	-1,	/* 102: Unused Number					*/
	-1,	/* 103: Unused Number					*/
	-1,	/* 104: Unused Number					*/
	-1,	/* 105: Unused Number					*/
	-1,	/* 106: Unused Number					*/
	-1,	/* 107: Unused Number					*/
	-1,	/* 108: Unused Number					*/
	-1,	/* 109: Unused Number					*/
	-1,	/* 110: Unused Number					*/
	-1,	/* 111: Unused Number					*/
	-1,	/* 112: Unused Number					*/
	-1,	/* 113: Unused Number					*/
	-1,	/* 114: Unused Number					*/
	-1,	/* 115: Unused Number					*/
	-1,	/* 116: Unused Number					*/
	-1,	/* 117: Unused Number					*/
	-1,	/* 118: Unused Number					*/
	-1,	/* 119: Unused Number					*/
	93,	/* 120: EPROTONOSUPPORT	-->  93: EPROTONOSUPPORT	*/
	94,	/* 121: ESOCKTNOSUPPORT	-->  94: ESOCKTNOSUPPORT	*/
	95,	/* 122: EOPNOTSUPP	-->  95: EOPNOTSUPP		*/
	96,	/* 123: EPFNOSUPPORT	-->  96: EPFNOSUPPORT		*/
	97,	/* 124: EAFNOSUPPORT	-->  97: EAFNOSUPPORT		*/
	98,	/* 125: EADDRINUSE	-->  98: EADDRINUSE		*/
	99,	/* 126: EADDRNOTAVAIL	-->  99: EADDRNOTAVAIL		*/
	100,	/* 127: ENETDOWN	--> 100: ENETDOWN		*/
	101,	/* 128: ENETUNREACH	--> 101: ENETUNREACH		*/
	102,	/* 129: ENETRESET	--> 102: ENETRESET		*/
	103,	/* 130: ECONNABORTED	--> 103: ECONNABORTED		*/
	104,	/* 131: ECONNRESET	--> 104: ECONNRESET		*/
	105,	/* 132: ENOBUFS		--> 105: ENOBUFS		*/
	106,	/* 133: EISCONN		--> 106: EISCONN		*/
	107,	/* 134: ENOTCONN	--> 107: ENOTCONN		*/
	-1,	/* 135: Unused Number					*/
	-1,	/* 136: Unused Number					*/
	-1,	/* 137: Unused Number					*/
	-1,	/* 138: Unused Number					*/
	-1,	/* 139: Unused Number					*/
	-1,	/* 140: Unused Number					*/
	-1,	/* 141: Unused Number					*/
	-1,	/* 142: Unused Number					*/
	108,	/* 143: ESHUTDOWN	--> 108: ESHUTDOWN		*/
	109,	/* 144: ETOOMANYREFS	--> 109: ETOOMANYREFS		*/
	110,	/* 145: ETIMEDOUT	--> 110: ETIMEDOUT		*/
	111,	/* 146: ECONNREFUSED	--> 111: ECONNREFUSED		*/
	112,	/* 147: EHOSTDOWN	--> 112: EHOSTDOWN		*/
	113,	/* 148: EHOSTUNREACH	--> 113: EHOSTUNREACH		*/
	114,	/* 149: EALREADY	--> 114: EALREADY		*/
	115,	/* 150: EINPROGRESS	--> 115: EINPROGRESS		*/
	116	/* 151: ESTALE		--> 116: ESTALE			*/
};

/*
 * Convert an illumos native error number to a Linux error number and return
 * it.  If no valid conversion is possible, the function fails back to the
 * value of "defval".  In userland, passing a default error number of "-1"
 * will abort the program if the error number could not be converted.
 */
int
lx_errno(int native_errno, int defval)
{
#ifdef	_KERNEL
	VERIFY3S(defval, >=, 0);
#endif

	if (native_errno < 0 || native_errno >= (sizeof (lx_stol_errno) /
	    sizeof (lx_stol_errno[0]))) {
#ifndef	_KERNEL
		VERIFY3S(defval, >=, 0);
#endif

		return (defval);
	}

	return (lx_stol_errno[native_errno]);
}
